{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "mCCNdiwtbrWe"
   },
   "source": [
    "# Retrieval-Augmented Generation(RAG) using IBM Watsonx"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "SPsPVUKIcMod"
   },
   "source": [
    "In this notebook, we will build a Retrieval-Augmented Generation(RAG) using IBM Watsonx embeddings and GPT 3.5 LLM .\n",
    "\n",
    "RAG is a technique that retrieves related documents to the user's question, combines them with LLM-base prompt, and sends them to LLMs like GPT to produce more factually accurate generation.\n",
    "\n",
    "In this example, we'll use `DeadPool and Wolverine` marvel page to build RAG around it."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "ii6LDwI4Aao0"
   },
   "source": [
    "![Screenshot from 2024-08-14 13-58-50.png]()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "QgjQ_nJ4guH4"
   },
   "source": [
    "## Install Dependencies"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "-m03pTRSFDSw"
   },
   "outputs": [],
   "source": [
    "# install lancedb and ibm-watson\n",
    "!pip install ibm-watsonx-ai lancedb -q"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "wkLpDUGU-2f-"
   },
   "outputs": [],
   "source": [
    "# install llama-index and web-reader\n",
    "!pip install llama_index llama-index-readers-web -q"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "FdXgSesMgvjW"
   },
   "source": [
    "## Load documents from webpage"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "jxDWeCUbNywa"
   },
   "outputs": [],
   "source": [
    "# Load data\n",
    "import argparse\n",
    "from llama_index.core import VectorStoreIndex, Settings, StorageContext\n",
    "from llama_index.readers.web import SimpleWebPageReader\n",
    "\n",
    "\n",
    "def get_doc_from_url(url):\n",
    "    \"\"\"\n",
    "    Load data from a url\n",
    "    \"\"\"\n",
    "    documents = SimpleWebPageReader(html_to_text=True).load_data([url])\n",
    "    return documents"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "mQ1O28V-ELfx",
    "outputId": "5e109780-4fc2-4be9-fb76-18c7d7604040"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n"
     ]
    }
   ],
   "source": [
    "# documents\n",
    "url = \"https://marvelcinematicuniverse.fandom.com/wiki/Deadpool_%26_Wolverine\"\n",
    "documents = get_doc_from_url(url)\n",
    "print(len(documents))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "JaiySs6Eg1Ys"
   },
   "source": [
    "## Chunking"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 127
    },
    "id": "ivyZYKvYbp8X",
    "outputId": "194a77c0-b074-42d9-adf8-e04b336fc178"
   },
   "outputs": [
    {
     "data": {
      "application/vnd.google.colaboratory.intrinsic+json": {
       "type": "string"
      },
      "text/plain": [
       "'# Marvel Cinematic Universe Wiki\\n\\nWith the release of _[Deadpool& Wolverine](/wiki/Deadpool_%26_Wolverine\\n\"Deadpool & Wolverine\")_ we\\'d like to remind the community of some things.\\nPlease DO NOT add info from the Fox X-Men movies when it comes to pages. Only\\ninformation stated within the movie itself is eligible to be added. In\\naddition, do not upload pirated images from cam copies. We\\'d also like to ask\\nthat universe pages NOT be created if the universe name has a Placeholder\\ntemplate on it, as that indicates it is not the intended final name. Thank\\nyou.\\n\\nREAD MORE\\n\\n[ ![Marvel Cinematic Universe\\nWiki](https://static.wikia.nocookie.net/marvelcinematicuniverse/images/e/e6/Site-\\nlogo.png/revision/latest?cb=20220123055921)\\n](//marvelcinematicuniverse.fandom.com) [Marvel Cinematic Universe\\nWiki](//marvelcinematicuniverse.fandom.com)\\n\\n  * Explore\\n\\n    * [ Main Page ](https://marvelcinematicuniverse.fandom.com/wiki/Marvel_Cinematic_Universe_Wiki)\\n    * [ Discuss ](/f)\\n    * [ All Pages ](https://marvelcinematicuniverse.fandom.com/wiki/Special:AllPages)\\n    * [ Community ](https://marvelcinematicuniverse.fandom.com/wiki/Special:Community)\\n    * [ Interactive Maps ](https://marvelcinematicuniverse.fandom.com/wiki/Special:AllMaps)\\n    * [ Recent Blog Posts ](/Blog:Recent_posts)\\n\\n  * [ Universe ](https://marvelcinematicuniverse.fandom.com/wiki/Marvel_Cinematic_Universe)\\n\\n    * [ Organizations ](https://marvelcinematicuniverse.fandom.com/wiki/Category:Organizations)\\n\\n      * [ S.H.I.E.L.D. ](https://marvelcinematicuniverse.fandom.com/wiki/S.H.I.E.L.D.)\\n      * [ HYDRA ](https://marvelcinematicuniverse.fandom.com/wiki/HYDRA)\\n      * [ Stark Industries ](https://marvelcinematicuniverse.fandom.com/wiki/Stark_Industries)\\n      * [ United States Armed Forces ](https://marvelcinematicuniverse.fandom.com/wiki/United_States_Armed_Forces)\\n      * [ Strategic Scientific Reserve ](https://marvelcinematicuniverse.fandom.com/wiki/Strategic_Scientific_Reserve)\\n      * [ Roxxon Oil Corporation ](https://marvelcinematicuniverse.fandom.com/wiki/Roxxon_Oil_Corporation)\\n      * [ New York City Police Department ](https://marvelcinematicuniverse.fandom.com/wiki/New_York_City_Police_Department)\\n      * [ more. ](https://marvelcinematicuniverse.fandom.com/wiki/Category:Organizations)\\n\\n    * [ Teams ](https://marvelcinematicuniverse.fandom.com/wiki/Category:Teams)\\n\\n      * [ Avengers ](https://marvelcinematicuniverse.fandom.com/wiki/Avengers)\\n      * [ Guardians of the Galaxy  ](https://marvelcinematicuniverse.fandom.com/wiki/Guardians_of_the_Galaxy)\\n      * [ Secret Warriors ](https://marvelcinematicuniverse.fandom.com/wiki/Secret_Warriors)\\n      * [ Defenders ](https://marvelcinematicuniverse.fandom.com/wiki/Defenders)\\n      * [ Runaways ](https://marvelcinematicuniverse.fandom.com/wiki/Runaways)\\n      * [ Thunderbolts ](https://marvelcinematicuniverse.fandom.com/wiki/Thunderbolts)\\n      * [ more. ](https://marvelcinematicuniverse.fandom.com/wiki/Category:Teams)\\n\\n    * [ Characters ](https://marvelcinematicuniverse.fandom.com/wiki/Category:Characters)\\n\\n      * [ Iron Man  ](https://marvelcinematicuniverse.fandom.com/wiki/Iron_Man)\\n      * [ Captain America ](https://marvelcinematicuniverse.fandom.com/wiki/Captain_America)\\n      * [ Thor  ](https://marvelcinematicuniverse.fandom.com/wiki/Thor)\\n      * [ Hulk ](https://marvelcinematicuniverse.fandom.com/wiki/Hulk)\\n      * [ Ant-Man  ](https://marvelcinematicuniverse.fandom.com/wiki/Ant-Man)\\n      * [ Spider-Man ](https://marvelcinematicuniverse.'"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from llama_index.core.node_parser import SentenceSplitter\n",
    "\n",
    "# splitting extracted document into chunks\n",
    "splitter = SentenceSplitter(\n",
    "    chunk_size=1024,\n",
    "    chunk_overlap=20,\n",
    ")\n",
    "nodes = splitter.get_nodes_from_documents(documents)\n",
    "nodes[0].text"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "EexMWZVcg43t"
   },
   "source": [
    "## Set Watson and OpenAI creds as env variable"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "zbLm3WT4dOf7"
   },
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "# set watson api key and project id and OpenAI key as env variable\n",
    "os.environ[\"WATSONX_API_KEY\"] = \"....\"\n",
    "os.environ[\"WATSONX_PROJECT_ID\"] = \"...\"\n",
    "\n",
    "os.environ[\"OPENAI_API_KEY\"] = \"sk-proj-...\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "reT0v1geErRB",
    "outputId": "4dbd9744-001a-4d0c-8975-7e39edcb10f7"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "OPENAI_API_KEY is already set\n",
      "WATSONX_API_KEY is already set\n",
      "WATSONX_PROJECT_ID is already set\n"
     ]
    }
   ],
   "source": [
    "# check for all env variables set or not\n",
    "if os.environ.get(\"OPENAI_API_KEY\") is not None:\n",
    "    print(\"OPENAI_API_KEY is already set\")\n",
    "else:\n",
    "    raise KeyError(\"OPENAI_API_KEY is not set as an env variable\")\n",
    "\n",
    "if os.environ.get(\"WATSONX_API_KEY\") is not None:\n",
    "    print(\"WATSONX_API_KEY is already set\")\n",
    "else:\n",
    "    raise KeyError(\"WATSONX_API_KEY is not set as an env variable\")\n",
    "\n",
    "if os.environ.get(\"WATSONX_PROJECT_ID\") is not None:\n",
    "    print(\"WATSONX_PROJECT_ID is already set\")\n",
    "else:\n",
    "    raise KeyError(\"WATSONX_PROJECT_ID is not set as an env variable\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "obiQCVanhFO7"
   },
   "source": [
    "## Define custom Embedding function for watsonx model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "CtGiq89o8Nf9"
   },
   "outputs": [],
   "source": [
    "from lancedb.embeddings.registry import register\n",
    "from lancedb.util import attempt_import_or_raise\n",
    "from functools import cached_property\n",
    "from lancedb.embeddings.base import TextEmbeddingFunction\n",
    "\n",
    "from ibm_watsonx_ai import Credentials\n",
    "from ibm_watsonx_ai.foundation_models import Embeddings\n",
    "from ibm_watsonx_ai.metanames import EmbedTextParamsMetaNames as EmbedParams\n",
    "from ibm_watsonx_ai.foundation_models.utils.enums import EmbeddingTypes\n",
    "\n",
    "# define IBM watsonx Embedding function\n",
    "\n",
    "\n",
    "@register(\"ibm-watsonx\")\n",
    "class WatsonEmbeddings(TextEmbeddingFunction):\n",
    "    def __init__(self, **kwargs):\n",
    "        super().__init__(**kwargs)\n",
    "        self._ndims = None\n",
    "        self._embed_params = {\n",
    "            EmbedParams.TRUNCATE_INPUT_TOKENS: 3,\n",
    "            EmbedParams.RETURN_OPTIONS: {\"input_text\": True},\n",
    "        }\n",
    "        self._watson_api_key = os.environ.get(\"WATSONX_API_KEY\")\n",
    "        self._watson_project_id = os.environ.get(\"WATSONX_PROJECT_ID\")\n",
    "        self._watson_credentials = Credentials(\n",
    "            api_key=self._watson_api_key, url=\"https://us-south.ml.cloud.ibm.com\"\n",
    "        )\n",
    "\n",
    "    def generate_embeddings(self, texts):\n",
    "        model = Embeddings(\n",
    "            model_id=EmbeddingTypes.IBM_SLATE_30M_ENG,\n",
    "            params=self._embed_params,\n",
    "            credentials=self._watson_credentials,\n",
    "            project_id=self._watson_project_id,\n",
    "        )\n",
    "        return model.embed_documents(texts=list(texts))\n",
    "\n",
    "    def ndims(self):\n",
    "        if self._ndims is None:\n",
    "            self._ndims = len(self.generate_embeddings(texts=[\"foo\"])[0])\n",
    "        return self._ndims"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "yIN_3PJRhO7m"
   },
   "source": [
    "## Create Table"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "wuz1Akkji4uG"
   },
   "outputs": [],
   "source": [
    "import os\n",
    "import lancedb\n",
    "from lancedb.pydantic import LanceModel, Vector\n",
    "from lancedb.embeddings import EmbeddingFunctionRegistry\n",
    "\n",
    "# create a table with above defined embedding function\n",
    "watsonx_embed = EmbeddingFunctionRegistry.get_instance().get(\"ibm-watsonx\").create()\n",
    "\n",
    "\n",
    "# schema of table\n",
    "class TextModel(LanceModel):\n",
    "    text: str = watsonx_embed.SourceField()\n",
    "    vector: Vector(watsonx_embed.ndims()) = watsonx_embed.VectorField()\n",
    "\n",
    "\n",
    "# data chunks\n",
    "data = []\n",
    "for node in nodes:\n",
    "    data.append({\"text\": node.text})\n",
    "\n",
    "db = lancedb.connect(\"~/.lancedb\")\n",
    "tbl = db.create_table(\"watsonx_test\", schema=TextModel, mode=\"overwrite\")\n",
    "\n",
    "tbl.add(data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "KwG4OgnWhUo5"
   },
   "source": [
    "## Query\n",
    "Query ingested webpage"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "tICLPJBneq3n"
   },
   "outputs": [],
   "source": [
    "query = \"role of deadpool\"\n",
    "result = tbl.search(query).limit(5).to_list()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "gB92PIaeCoxh",
    "outputId": "8ea9475a-da2f-4964-d764-93e65e449226"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Despite\\nDeadpool\\'s antagonism towards Nicepool, the latter gives them directions to\\nthe Void\\'s borderlands and provides them with a car to make it there. While\\ndriving, Deadpool accidentally slips up that he is not certain that the TVA\\nwill fix Wolverine\\'s reality, admitting that he made an \"educated wish\".\\nWolverine is enraged and lashes out at Deadpool, which results in both of them\\nfighting each other until both are too exhausted, and someone drives their car\\nto the Resistance hideout.\\n\\n[![Deadpool & Wolverine Trailer\\n\\\\(64\\\\)](%3D%3D)](https://static.wikia.nocookie.net/marvelcinematicuniverse/images/1/13/Deadpool_%26_Wolverine_Trailer_%2864%29.png/revision/latest?cb=20240422182040)\\n[![Deadpool & Wolverine Trailer\\n\\\\(64\\\\)](https://static.wikia.nocookie.net/marvelcinematicuniverse/images/1/13/Deadpool_%26_Wolverine_Trailer_%2864%29.png/revision/latest/scale-\\nto-width-\\ndown/250?cb=20240422182040)](https://static.wikia.nocookie.net/marvelcinematicuniverse/images/1/13/Deadpool_%26_Wolverine_Trailer_%2864%29.png/revision/latest?cb=20240422182040)\\n[](/wiki/File:Deadpool_%26_Wolverine_Trailer_\\\\(64\\\\).png)\\n\\n[Wolverine](/wiki/Wolverine \"Wolverine\") reflects on his past\\n\\nThere, they meet [Elektra](/wiki/Elektra/Forgotten_Elektra \"Elektra/Forgotten\\nElektra\"), [Blade](/wiki/Blade/Universe_Defender_Blade \"Blade/Universe\\nDefender Blade\"), and [Gambit](/wiki/Gambit \"Gambit\") variants, led by X-23\\nwho had been banished to the Void by the TVA. Deadpool convinces the\\nResistance to team up and take on Nova and avenge Storm\\'s death, while leaving\\nout the reason why he died. Wolverine scoffs that their plan is doomed to fail\\nand refuses to join them, so X-23 goes to talk to him. Wolverine admits that\\nin his universe, he abandoned his fellow X-Men, which resulted in them being\\nkilled by the anti-mutant vigilantes, much to his horror and despair, so he\\nkeeps wearing the X-Man suit since then. X-23 tells Wolverine that regardless\\nof what he thinks about himself, he is still a good person at heart and always\\nwill be. Meanwhile, Pyro follows Deadpool and Wolverine\\'s tracks and strikes a\\ndeal with Paradox, who decides that Nova is too dangerous and must be\\neliminated.\\n\\n[![Cassandra Nova - Boys Are So\\nSilly](%3D%3D)](https://static.wikia.nocookie.net/marvelcinematicuniverse/images/2/23/Cassandra_Nova_-\\n_Boys_Are_So_Silly.jpg/revision/latest?cb=20240422202104) [![Cassandra Nova -\\nBoys Are So\\nSilly](https://static.wikia.nocookie.net/marvelcinematicuniverse/images/2/23/Cassandra_Nova_-\\n_Boys_Are_So_Silly.jpg/revision/latest/scale-to-width-\\ndown/250?cb=20240422202104)](https://static.wikia.nocookie.net/marvelcinematicuniverse/images/2/23/Cassandra_Nova_-\\n_Boys_Are_So_Silly.jpg/revision/latest?cb=20240422202104)\\n[](/wiki/File:Cassandra_Nova_-_Boys_Are_So_Silly.jpg)\\n\\n[Nova](/wiki/Cassandra_Nova \"Cassandra Nova\") meets [Deadpool](/wiki/Deadpool\\n\"Deadpool\") and [Wolverine](/wiki/Wolverine \"Wolverine\") again\\n\\nWith Wolverine joining them, the Resistance storms Nova\\'s base, planning to\\nacquire [Juggernaut](/wiki/Juggernaut \"Juggernaut\")\\'s\\n[helmet](/wiki/Juggernaut%27s_Helmet \"Juggernaut\\'s Helmet\") to neutralize\\nNova\\'s psychic ability.', '[Deadpool & Wolverine - Official Teaser - In\\nTheaters July\\n26](%3D%3D)](/wiki/File:Deadpool_%26_Wolverine_-\\n_Official_Teaser_-_In_Theaters_July_26 \"Deadpool & Wolverine - Official Teaser\\n- In Theaters July 26 \\\\(19 KB\\\\)\")\\n\\nDeadpool & Wolverine - Official Teaser - In Theaters July 26\\n\\n[![Deadpool & Wolverine - Official Trailer - In Theaters July\\n26](https://static.wikia.nocookie.net/marvelcinematicuniverse/images/3/3a/Deadpool_%26_Wolverine_-\\n_Official_Trailer_-_In_Theaters_July_26/revision/latest/scale-to-width-\\ndown/185?cb=20240422130039)![Deadpool & Wolverine - Official Trailer - In\\nTheaters July\\n26](%3D%3D)](/wiki/File:Deadpool_%26_Wolverine_-\\n_Official_Trailer_-_In_Theaters_July_26 \"Deadpool & Wolverine - Official\\nTrailer - In Theaters July 26 \\\\(33 KB\\\\)\")\\n\\nDeadpool & Wolverine - Official Trailer - In Theaters July 26\\n\\n[![Deadpool & Wolverine - Final Trailer - In Theaters July\\n26](https://static.wikia.nocookie.net/marvelcinematicuniverse/images/c/cd/Deadpool_%26_Wolverine_-\\n_Final_Trailer_-_In_Theaters_July_26/revision/latest/scale-to-width-\\ndown/185?cb=20240719204927)![Deadpool & Wolverine - Final Trailer - In\\nTheaters July\\n26](%3D%3D)](/wiki/File:Deadpool_%26_Wolverine_-\\n_Final_Trailer_-_In_Theaters_July_26 \"Deadpool & Wolverine - Final Trailer -\\nIn Theaters July 26 \\\\(27 KB\\\\)\")\\n\\nDeadpool & Wolverine - Final Trailer - In Theaters July 26\\n\\n### TV Spots[]\\n\\n[![Deadpool & Wolverine - Nice - In Theaters July\\n26](https://static.wikia.nocookie.net/marvelcinematicuniverse/images/b/bb/Deadpool_%26_Wolverine_-\\n_Nice_-_In_Theaters_July_26/revision/latest/scale-to-width-\\ndown/185?cb=20240628131608)![Deadpool & Wolverine - Nice - In Theaters July\\n26](%3D%3D)](/wiki/File:Deadpool_%26_Wolverine_-\\n_Nice_-_In_Theaters_July_26 \"Deadpool & Wolverine - Nice - In Theaters July 26\\n\\\\(23 KB\\\\)\")\\n\\nDeadpool & Wolverine - Nice - In Theaters July 26\\n\\n[![Deadpool & Wolverine - LFG - In Theaters July\\n26](https://static.wikia.nocookie.net/marvelcinematicuniverse/images/3/37/Deadpool_%26_Wolverine_-\\n_LFG_-_In_Theaters_July_26/revision/latest/scale-to-width-\\ndown/185?cb=20240701160427)![Deadpool & Wolverine - LFG - In Theaters July\\n26](%3D%3D)](/wiki/File:Deadpool_%26_Wolverine_-\\n_LFG_-_In_Theaters_July_26 \"Deadpool & Wolverine - LFG - In Theaters July 26\\n\\\\(25 KB\\\\)\")\\n\\nDeadpool & Wolverine - LFG - In Theaters July 26\\n\\n[![Deadpool & Wolverine - Purpose - In Theaters July\\n26](https://static.wikia.nocookie.net/marvelcinematicuniverse/images/e/e0/Deadpool_%26_Wolverine_-\\n_Purpose_-_In_Theaters_July_26/revision/latest/scale-to-width-\\ndown/185?cb=20240709163816)!', '[Deadpool & Wolverine - Gear Up - In Theaters July\\n26](%3D%3D)](/wiki/File:Deadpool_%26_Wolverine_-\\n_Gear_Up_-_In_Theaters_July_26 \"Deadpool & Wolverine - Gear Up - In Theaters\\nJuly 26 \\\\(26 KB\\\\)\")\\n\\nDeadpool & Wolverine - Gear Up - In Theaters July 26\\n\\n[![Deadpool & Wolverine - Pray - In Theaters July\\n26](https://static.wikia.nocookie.net/marvelcinematicuniverse/images/0/09/Deadpool_%26_Wolverine_-\\n_Pray_-_In_Theaters_July_26/revision/latest/scale-to-width-\\ndown/185?cb=20240724155959)![Deadpool & Wolverine - Pray - In Theaters July\\n26](%3D%3D)](/wiki/File:Deadpool_%26_Wolverine_-\\n_Pray_-_In_Theaters_July_26 \"Deadpool & Wolverine - Pray - In Theaters July 26\\n\\\\(21 KB\\\\)\")\\n\\nDeadpool & Wolverine - Pray - In Theaters July 26\\n\\n[![Deadpool & Wolverine - Go - In Theaters July\\n26](https://static.wikia.nocookie.net/marvelcinematicuniverse/images/0/07/Deadpool_%26_Wolverine_-\\n_Go_-_In_Theaters_July_26/revision/latest/scale-to-width-\\ndown/185?cb=20240725155613)![Deadpool & Wolverine - Go - In Theaters July\\n26](%3D%3D)](/wiki/File:Deadpool_%26_Wolverine_-\\n_Go_-_In_Theaters_July_26 \"Deadpool & Wolverine - Go - In Theaters July 26\\n\\\\(25 KB\\\\)\")\\n\\nDeadpool & Wolverine - Go - In Theaters July 26\\n\\n[![Deadpool & Wolverine - Legendary - In Theaters July\\n26](https://static.wikia.nocookie.net/marvelcinematicuniverse/images/7/71/Deadpool_%26_Wolverine_-\\n_Legendary_-_In_Theaters_July_26/revision/latest/scale-to-width-\\ndown/185?cb=20240725192544)![Deadpool & Wolverine - Legendary - In Theaters\\nJuly\\n26](%3D%3D)](/wiki/File:Deadpool_%26_Wolverine_-\\n_Legendary_-_In_Theaters_July_26 \"Deadpool & Wolverine - Legendary - In\\nTheaters July 26 \\\\(23 KB\\\\)\")\\n\\nDeadpool & Wolverine - Legendary - In Theaters July 26\\n\\n[![Deadpool & Wolverine - Smash - Now Playing In\\nTheaters](https://static.wikia.nocookie.net/marvelcinematicuniverse/images/c/c5/Deadpool_%26_Wolverine_-\\n_Smash_-_Now_Playing_In_Theaters/revision/latest/scale-to-width-\\ndown/185?cb=20240802172000)![Deadpool & Wolverine - Smash - Now Playing In\\nTheaters](%3D%3D)](/wiki/File:Deadpool_%26_Wolverine_-\\n_Smash_-_Now_Playing_In_Theaters \"Deadpool & Wolverine - Smash - Now Playing\\nIn Theaters \\\\(32 KB\\\\)\")\\n\\nDeadpool & Wolverine - Smash - Now Playing In Theaters\\n\\n[![Deadpool & Wolverine - Crying - Now Playing In\\nTheaters](https://static.wikia.nocookie.net/marvelcinematicuniverse/images/e/ef/Deadpool_%26_Wolverine_-\\n_Crying_-_Now_Playing_In_Theaters/revision/latest/scale-to-width-\\ndown/185?cb=20240803164425)!', '[Deadpool & Wolverine - Crying - Now Playing In\\nTheaters](%3D%3D)](/wiki/File:Deadpool_%26_Wolverine_-\\n_Crying_-_Now_Playing_In_Theaters \"Deadpool & Wolverine - Crying - Now Playing\\nIn Theaters \\\\(21 KB\\\\)\")\\n\\nDeadpool & Wolverine - Crying - Now Playing In Theaters\\n\\n[![Deadpool & Wolverine - Smashing - Now Playing In\\nTheaters](https://static.wikia.nocookie.net/marvelcinematicuniverse/images/8/8a/Deadpool_%26_Wolverine_-\\n_Smashing_-_Now_Playing_In_Theaters/revision/latest/scale-to-width-\\ndown/185?cb=20240808200314)![Deadpool & Wolverine - Smashing - Now Playing In\\nTheaters](%3D%3D)](/wiki/File:Deadpool_%26_Wolverine_-\\n_Smashing_-_Now_Playing_In_Theaters \"Deadpool & Wolverine - Smashing - Now\\nPlaying In Theaters \\\\(29 KB\\\\)\")\\n\\nDeadpool & Wolverine - Smashing - Now Playing In Theaters\\n\\n[![Deadpool & Wolverine - Portal - Now Playing In\\nTheaters](https://static.wikia.nocookie.net/marvelcinematicuniverse/images/b/b3/Deadpool_%26_Wolverine_-\\n_Portal_-_Now_Playing_In_Theaters/revision/latest/scale-to-width-\\ndown/185?cb=20240809231007)![Deadpool & Wolverine - Portal - Now Playing In\\nTheaters](%3D%3D)](/wiki/File:Deadpool_%26_Wolverine_-\\n_Portal_-_Now_Playing_In_Theaters \"Deadpool & Wolverine - Portal - Now Playing\\nIn Theaters \\\\(27 KB\\\\)\")\\n\\nDeadpool & Wolverine - Portal - Now Playing In Theaters\\n\\n[![Deadpool & Wolverine - Flame On - Now Playing In\\nTheaters](https://static.wikia.nocookie.net/marvelcinematicuniverse/images/f/f6/Deadpool_%26_Wolverine_-\\n_Flame_On_-_Now_Playing_In_Theaters/revision/latest/scale-to-width-\\ndown/185?cb=20240810203449)![Deadpool & Wolverine - Flame On - Now Playing In\\nTheaters](%3D%3D)](/wiki/File:Deadpool_%26_Wolverine_-\\n_Flame_On_-_Now_Playing_In_Theaters \"Deadpool & Wolverine - Flame On - Now\\nPlaying In Theaters \\\\(20 KB\\\\)\")\\n\\nDeadpool & Wolverine - Flame On - Now Playing In Theaters\\n\\n[![Deadpool & Wolverine - Billion with a\\nB](https://static.wikia.nocookie.net/marvelcinematicuniverse/images/3/39/Deadpool_%26_Wolverine_-\\n_Billion_with_a_B/revision/latest/scale-to-width-\\ndown/185?cb=20240811170817)![Deadpool & Wolverine - Billion with a\\nB](%3D%3D)](/wiki/File:Deadpool_%26_Wolverine_-\\n_Billion_with_a_B \"Deadpool & Wolverine - Billion with a B \\\\(23 KB\\\\)\")\\n\\nDeadpool & Wolverine - Billion with a B\\n\\n### Other[]\\n\\n[![Deadpool\\nUpdate](https://static.wikia.nocookie.net/marvelcinematicuniverse/images/f/f1/Deadpool_Update/revision/latest/scale-\\nto-width-down/185?cb=20220927214513)![Deadpool\\nUpdate](%3D%3D)](/wiki/File:Deadpool_Update\\n\"Deadpool Update \\\\(28 KB\\\\)\")\\n\\nDeadpool Update\\n\\n[!', '[Deadpool & Wolverine Trailer\\n\\\\(73\\\\)](https://static.wikia.nocookie.net/marvelcinematicuniverse/images/5/5b/Deadpool_%26_Wolverine_Trailer_%2873%29.png/revision/latest/scale-\\nto-width-\\ndown/250?cb=20240422182041)](https://static.wikia.nocookie.net/marvelcinematicuniverse/images/5/5b/Deadpool_%26_Wolverine_Trailer_%2873%29.png/revision/latest?cb=20240422182041)\\n[](/wiki/File:Deadpool_%26_Wolverine_Trailer_\\\\(73\\\\).png)\\n\\n[Deadpool](/wiki/Deadpool \"Deadpool\") and [Wolverine](/wiki/Wolverine\\n\"Wolverine\") confront the [Deadpool Corps](/wiki/Deadpool_Corps \"Deadpool\\nCorps\")\\n\\nDeadpool and Wolverine fight through the hordes of Deadpool variants, who keep\\ncoming back due to their regeneration, however, they all cease fighting once\\nthey see [Peter Wisdom](/wiki/Peter_Wisdom \"Peter Wisdom\"), who is deeply\\nloved by all Deadpools. With them distracted, Deadpool and Wolverine make it\\nto Paradox who tells them that Nova already took control over the Time Ripper\\nand is preparing to unravel the entire Multiverse. He tells them that the only\\nway to stop this is to short-circuit the chambers of matter and anti-matter\\nthat power the device, however, it is all but guaranteed to kill them even\\nwith their regeneration. Wolverine says he should be the one to do it,\\nclaiming that he has nothing to fight for anymore, however, Deadpool locks him\\nout of the bunker and prepares to sacrifice himself instead.\\n\\nDespite Wolverine\\'s protests, Deadpool decides that he must do it himself to\\nsave everyone he loves, however, Wolverine breaks in and connects the chamber\\nwith him, resulting in the destruction of the Time Ripper and Nova\\'s death.\\n[B-15](/wiki/B-15 \"B-15\") and the TVA come to investigate the incident, with\\nParadox claiming that Nova is solely responsible while mourning the deaths of\\nDeadpool and Wolverine, however, they manage to survive by sharing the energy\\nburst between each other. Paradox gets taken under custody and Deadpool asks\\nB-15 to do good by their Resistance allies who were left in the Void and fix\\nWolverine\\'s home reality. B-15 promises to consider the former but notes that\\nWolverine\\'s unfortunate circumstances were what led him to save the\\nMultiverse.\\n\\n[![Wade smiling at\\nVanessa](%3D%3D)](https://static.wikia.nocookie.net/marvelcinematicuniverse/images/3/36/Wade_smiling_at_Vanessa.png/revision/latest?cb=20240723211721)\\n[![Wade smiling at\\nVanessa](https://static.wikia.nocookie.net/marvelcinematicuniverse/images/3/36/Wade_smiling_at_Vanessa.png/revision/latest/scale-\\nto-width-\\ndown/250?cb=20240723211721)](https://static.wikia.nocookie.net/marvelcinematicuniverse/images/3/36/Wade_smiling_at_Vanessa.png/revision/latest?cb=20240723211721)\\n[](/wiki/File:Wade_smiling_at_Vanessa.png)\\n\\n[Wade](/wiki/Wade_Wilson \"Wade Wilson\") and [Vanessa](/wiki/Vanessa_Carlysle\\n\"Vanessa Carlysle\") rekinlde their relationship\\n\\nWilson and Logan then go on to have some shawarma and get reunited with\\nDogpool, now that Nicepool got killed in the battle. Wilson takes both of them\\nto his place with [Blind Al](/wiki/Blind_Al \"Blind Al\"), inviting their\\nfriends and [X-23](/wiki/X-23 \"X-23\") to hang out together, and Wilson\\nrekindles his relationship with Carlysle. Later on, Deadpool pulls up the\\nfootage of Storm actually cursing out Nova, proving that he did not lie or set\\nhim up to be killed.']\n"
     ]
    }
   ],
   "source": [
    "context = [r[\"text\"] for r in result]\n",
    "print(context)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "aHkefMF9hz3E"
   },
   "source": [
    "## LLM using Chain\n",
    "Set up a process where you use a prompt and OpenAI's language model to generate responses based on the content of the documents you provide."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "ttCnVGGugatN"
   },
   "outputs": [],
   "source": [
    "# Context Prompt\n",
    "\n",
    "base_prompt = \"\"\"You are an AI assistant. Your task is to understand the user question, and provide an answer using the provided contexts. Every answer you generate should have citations in this pattern  \"Answer [position].\", for example: \"Earth is round [1][2].,\" if it's relevant.\n",
    "\n",
    "Your answers are correct, high-quality, and written by an domain expert. If the provided context does not contain the answer, simply state, \"The provided context does not have the answer.\"\n",
    "\n",
    "User question: {}\n",
    "\n",
    "Contexts:\n",
    "{}\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "RdnqF9RpD6Qe",
    "outputId": "5259eabb-1e9c-4a5b-edf4-3b2192c9dc73"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The role of Deadpool in the events described involves teaming up with Wolverine and other characters to take on Nova and avenge Storm's death. Deadpool plays a crucial part in convincing the Resistance to join forces and confront Nova, ultimately leading to the destruction of the Time Ripper and Nova's demise. Despite Deadpool's initial intention to sacrifice himself, Wolverine also plays a significant role in the final act, resulting in both characters surviving the ordeal and being commended for their actions by B-15 and the TVA [1].\n"
     ]
    }
   ],
   "source": [
    "import openai\n",
    "\n",
    "# OpenAI chat completion for using context provided and generating answer\n",
    "prompt = f\"{base_prompt.format(query, context)}\"\n",
    "response = openai.chat.completions.create(\n",
    "    model=\"gpt-4o\",\n",
    "    temperature=0,\n",
    "    messages=[\n",
    "        {\"role\": \"system\", \"content\": prompt},\n",
    "    ],\n",
    ")\n",
    "\n",
    "print(response.choices[0].message.content)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "djSfhFMAikoh"
   },
   "source": [
    "## Conclusion\n",
    "\n",
    "By the end of this example, we’ve created a Retrieval-Augmented Generation (RAG) system for webpages. We showed how it works with pages about `Deadpool and Wolverine` from Marvel. Here’s how we did it in 4 simple steps:\n",
    "\n",
    "1. Load data from the webpage.\n",
    "2. Create a custom embedding function with IBM Watsonx.\n",
    "3. Add the loaded documents into a table with their embeddings.\n",
    "4. Use ChatChain to generate answers based on the retrieved documents."
   ]
  }
 ],
 "metadata": {
  "colab": {
   "provenance": []
  },
  "kernelspec": {
   "display_name": "Python 3",
   "name": "python3"
  },
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
